<template>
  <div>
    <div class="header-font">
      <Icon type="md-apps"/>
      <span>任务设置</span>
    </div>
    <div class="line"></div>
    <div>
      <Form ref="formInline" :model="payload" :label-width="80" inline>
        <FormItem prop="company" label="商家名称">
          <Input type="text" v-model="payload.company" placeholder="公司名称" style="width:200px"></Input>
        </FormItem>
        <FormItem prop="password" label="分享日期">
          <DatePicker
            type="daterange"
            v-model="payload.ctime"
            placeholder="分享日期"
            style="width: 200px"
          ></DatePicker>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="handleSubmit('formInline')">查询</Button>
        </FormItem>
      </Form>
    </div>
    <Table class="table" :columns="columns" :data="configtask" :loading="loading" border></Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize"
    ></table-page>
    <Modal v-model="modal" width="550px;" :mask-closable="false">
      <p slot="header" style="color:#f60;text-align:center">
        <Icon type="ios-information-circle"></Icon>
        <span>编辑任务数据</span>
      </p>
      <div style="text-align:left">
        <p :title="postparams.name" class="modal-task-name">任务标题：{{postparams.name}}</p>
        <Row class="modal-field-label">
          <Col class="field-label-font">阅读量：</Col>
          <Col span="18">
            <Input v-model="postparams.readcnt" style="width: 300px"/>
          </Col>
        </Row>
        <Row class="modal-field-label">
          <Col class="field-label-font">转发量：</Col>
          <Col span="18">
            <Input v-model="postparams.sharecnt" style="width: 300px"/>
          </Col>
        </Row>
      </div>
      <div slot="footer">
        <Button type="success" size="large" @click="changeTaskData">保存</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import TablePage from '_c/table-page'
import { mapActions, mapState } from 'vuex'
import { changeTaskData } from '../../../api/task'
export default {
  components: {
    TablePage
  },
  computed: {
    ...mapState({
      configtask: state => state.configtask.configtask,
      dataCount: state => state.configtask.configTaskTotalCount,
      historyData: state => state.configtask.configtaskHistoryData,
      pageSize: state => state.configtask.configTaskPageSize,
      currentPage: state => state.configtask.currentPage
    })
  },
  data () {
    return {
      payload: {
        company: '',
        ctime: ''
      },
      modal: false,
      page: 1,
      row: null,
      rows: 20,
      loading: true,
      postparams: {
        id: null,
        name: '',
        sharecnt: null,
        readcnt: null
      },
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 60,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '任务名称',
          key: 'name',
          align: 'center',
          minWidth: 180,
          render: (h, params) => {
            if (params.row.name && params.row.name.length > 8) {
              return h(
                'span',
                {
                  attrs: {
                    title: params.row.name
                  }
                },
                params.row.name ? params.row.name.slice(0, 8) + '...' : '-'
              )
            } else {
              return h('span', params.row.name ? params.row.name : '-')
            }
          }
        },
        {
          title: '商户管理员姓名',
          key: 'uname',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '商户电话',
          key: 'tel',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '商户名称',
          key: 'company',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '分享时间',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '分享量',
          key: 'sharecnt',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '阅读量',
          key: 'readcnt',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '操作',
          key: 'cz',
          minWidth: 179,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small',
                    ghost: 'ghost'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.handleClick(params.row)
                    }
                  }
                },
                '编辑'
              )
            ])
          }
        }
      ]
    }
  },
  created () {
    this.getConfigTaskData({}).then(res => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  },
  methods: {
    ...mapActions([
      'getConfigTaskData',
      'changeConfigTaskPage',
      'changeConfigTaskPageSize',
      'changeCurrentPage'
    ]),

    handleClick (row) {
      const { id, name, readcnt, sharecnt } = row
      this.postparams = {
        id,
        name,
        readcnt,
        sharecnt
      }
      this.modal = true
    },
    handleSubmit () {
      this.loading = true
      if (this.payload.ctime.filter(item => item !== '').length > 0) {
        let time1 = this.$moment(this.payload.ctime[0]).format('YYYY-MM-DD')
        let time2 = this.$moment(this.payload.ctime[1]).format('YYYY-MM-DD')
        this.payload.ctime = time1 + ',' + time2
      }

      this.getConfigTaskData(this.payload).then(res => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeConfigTaskPage(index)
      this.getConfigTaskData({
        page: index,
        rows: this.rows,
        ...this.payload
      }).then(res => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeConfigTaskPageSize(value)
      this.changePage(this.page)
      this.getConfigTaskData({
        rows: value,
        ...this.payload
      }).then(res => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    async changeTaskData () {
      const resp = await changeTaskData(this.postparams)
      this.modal = false
      if (resp.data.state === '000') {
        this.$Message.success('保存成功！')
        this.getConfigTaskData({}).then(res => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.warning('保存失败！')
      }
    }
  }
}
</script>

<style scoped>
.header-font {
  font-size: 15px;
  font-weight: bold;
}
.line {
  margin: 10px 0;
  width: 100%;
  border: 0.5px solid rgb(218, 218, 218);
}
.modal-task-name {
  font-size: 20px;
  font-weight: bold;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 500px;
}
.modal-field-label {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin-top: 20px;
}
.field-label-font {
  font-size: 18px;
  font-weight: bold;
}
</style>
